﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using Kunlun.Models;
using System.Configuration;
namespace Kunlun.BusinessLogic
{
    public class KunlunUploadFile
    {
        KunLunEntities entities = new KunLunEntities();
        private string path = System.Configuration.ConfigurationManager.AppSettings["photopath"];
        public void SaveFile(HttpPostedFileBase file,string libid,string ip)
        {
           
            
           // try
            //{
                int id = int.Parse(libid);
                Consumer_library lib = entities.Consumer_library.Single(l => l.ID == id);
                string monthpath = String.Format("{0}-{1}\\", DateTime.Now.Year, DateTime.Now.Month);
                string userpath = String.Format("{0}-{1}\\{2}\\", DateTime.Now.Year, DateTime.Now.Month, lib.UserID);
                 if (!Directory.Exists(path+monthpath))
                 {

                     Directory.CreateDirectory(path+monthpath);
                 }
                if (!Directory.Exists(path+userpath))
                 {

                     Directory.CreateDirectory(path+userpath);
                 }
                Consumer_pic pic = new Consumer_pic();
                pic.UserID = lib.UserID;
                pic.ImageAlt=ImageAlt(file.FileName);
                string Ext = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1).ToLower();
                if (PassUpload(Ext))
                {
                    string filename = String.Format("{0}{1}.{2}", DateTime.Now.Millisecond, new Random().Next(9999), Ext);
                    string ImageUrl = userpath + filename;
                    string ListPicUrl = String.Format("{0}m{1}", userpath, filename);
                    string ThumUrl = String.Format("{0}s{1}", userpath, filename);
                    file.SaveAs(path+ImageUrl);
                    pic.Md5=FileMD5.MD5(path + ImageUrl);
                    pic.ImageUrl = ImageUrl.Replace("\\","/");
                    pic.ListPicUrl = ListPicUrl.Replace("\\", "/"); ;
                    pic.Space = file.ContentLength / 1024;
                    pic.ThumUrl = ThumUrl.Replace("\\", "/"); ;
                    PhotoChangeSize photo = new PhotoChangeSize();
                    photo.OldFile=path+ImageUrl;
                    photo.NewFile=path+ListPicUrl;
                    photo.ChangSize(1000);
                    pic.Width = photo.Width;
                    pic.Height = photo.Height;
                    photo.NewFile = path + ThumUrl;
                    photo.ChangSize(120);
                    pic.LastUpdateTime = DateTime.Now;
                    pic.Library = lib.ID;
                    pic.BeginDate = DateTime.Now;
                    pic.Status = 1;
                    pic.BeginIP = ip;
                    entities.Consumer_pic.Add(pic);
                    entities.SaveChanges();


                }
                
           // }
            //catch (Exception ex)
           // {
           //     WebLog.ErrorLog(ex);
           // }
        }
        private bool PassUpload(string ext)
        {
            string[] Ext = System.Configuration.ConfigurationManager.AppSettings["Ext"].Split('.');
            int leng = Ext.Length;
            for (int i = 0; i < leng; i++)
            {
                if (Ext[i] == ext)
                {
                    return true;
                }
            }
            return false;

        }
        private string ImageAlt(string filename)
        {
            string[] file = filename.Split('.');
            return file[0];
        }
    }
}
